import Vuex from "vuex"
import user from "./modules/user.js";
import home from "./modules/home.js";
import createPersistedState from 'vuex-persistedstate'
import {getMemberInfo} from "@/api/user.js";
const store = new Vuex.Store({
    getters:{
        contract:({home}) => {
            return {
                aprove_address:home.aprove_address,
                ust_address:home.usdt_address,
                chin:home.chain,
                appid:home.app_id
            }
        },
        isProve:({user})=>{
            return user.account?.status === 1
        }
    },
    modules: {
        user: {
            namespaced: true,
            ...user
        },
        home: {
            namespaced: true,
            ...home
        }
    },
    actions:{
        updateMemberInfo({commit}){
            getMemberInfo().then(res=>{
                commit('user/setaccount',res)
            })
        }
    },
    plugins: [createPersistedState({
        storage: window.localStorage,
        key: 'clearusdt14'
    })]
})
store.watch((state) => state.user.account, (newVal, oldVal) => {
    if (newVal !== oldVal) {
        const {id,address} = newVal
        if (id && window?.ssq){
            ssq.push('setLoginInfo', {
                user_id:id,
                user_name:address,
                address:address,
            });
        }
    }
})
export default store